function handles = import_gui_kernel(wzrd)
% page for setting specimen symmetry

pos = get(wzrd,'Position');
h = pos(4);
w = pos(3);
ph = 270;

handles = getappdata(wzrd,'handles');

this_page = get_panel(w,h,ph);
handles.pages = [handles.pages,this_page];
setappdata(this_page,'pagename','Set Smoothing Kernel');

set(this_page,'visible','off');

kg = uibuttongroup('title','Smoothing Kernel',...
  'Parent',this_page,...
  'units','pixels','position',[0 0 w-20 h-120]);

%% ODF approximation

handles.method(1) = uicontrol(...
  'Parent',kg,...
  'Style','radio',...
  'String','ODF is given by function values at grid points',...
  'Value',0,...
  'position',[10 40 w-50 20]);

handles.method(2) = uicontrol(...
  'Parent',kg,...
  'Style','radio',...
  'String','ODF is given by accordingly distributed orientations ',...
  'Value',0,...
  'position',[10 10 w-50 20]);

%% kernel smoothing

uicontrol(...
 'Parent',kg,...
  'String','Kernel type',...
  'HitTest','off',...
  'Style','text',...
  'HorizontalAlignment','left',...
  'Position',[10 h-170 200 15]);


knames = kernel('names');
rm = cellfun(@(s) strmatch(s,knames),{'Laplace','Fourier','user'});
knames(rm) = [];

handles.kernel = uicontrol(...
  'Parent',kg,...
  'BackgroundColor',[1 1 1],...
  'FontName','monospaced',...
  'HorizontalAlignment','left',...
  'Position',[10 h-200 190 20],...
  'String',blanks(0),...
  'Style','popup',...
  'String',knames,...
  'Callback',@update_kernel,...
  'Value',1);

uicontrol(...
 'Parent',kg,...
  'String','Halfwidth',...
  'HitTest','off',...
  'Style','text',...
  'HorizontalAlignment','left',...
  'Position',[10 h-240 90 15]);

handles.halfwidth = uicontrol(...
  'Parent',kg,...
  'BackgroundColor',[1 1 1],...
  'FontName','monospaced',...
  'HorizontalAlignment','right',...
  'Position',[110 h-240 90 22],...
  'String','5',...
  'Callback',@update_kernel,...
  'Style','edit');

handles.exact = uicontrol(...
  'Parent',kg,...
  'Style','check',...
  'String','use binning',...
  'Value',1,...
  'position',[10 h-275 190 20]);


uicontrol(...
  'Parent',kg,...
  'Style','text',...
  'String','binning width',...
  'HitTest','off',...
  'HorizontalAlignment','left',...
  'position',[10 h-305 90 15]);

handles.approx = uicontrol(...
  'Parent',kg,...
  'BackgroundColor',[1 1 1],...
  'FontName','monospaced',...
  'HorizontalAlignment','right',...
  'Position',[110 h-305 90 22],...
  'String','5',...
  'Style','edit');




%% kernel plot

handles.kernelAxis = axes(...
  'Parent',kg,...
  'Units','pixels',...
  'Position',[210 90 w-250 h-240],...
  'yTick',[],...
  'box','on');

setappdata(this_page,'goto_callback',@goto_callback);
setappdata(this_page,'leave_callback',@leave_callback);
setappdata(wzrd,'handles',handles);


%% -------------- Callbacks ---------------------------------

function update_kernel(varargin)

handles = getappdata(gcbf,'handles');
knames = get(handles.kernel,'string');
kname = get(handles.kernel,'value');

hw = str2double(get(handles.halfwidth,'string'))*degree;

k = kernel(knames{kname},'halfwidth',hw);

plotkernel(gcbf,k);

function goto_callback(varargin)

handles = getappdata(gcbf,'handles');

data = getappdata(gcbf,'data');
k = get(data,'psi');

knames = get(handles.kernel,'string');
set(handles.kernel,'value',find(strcmp(get(k,'name'),knames)));
set(handles.halfwidth,'string',xnum2str(get(k,'hw')/degree));

plotkernel(gcbf,k);


function leave_callback(varargin)

handles = getappdata(gcbf,'handles');

knames = get(handles.kernel,'string');
kname = get(handles.kernel,'value');

hw = str2double(get(handles.halfwidth,'string'))*degree;

k = kernel(knames{kname},'halfwidth',hw);
data = getappdata(gcbf,'data');
data = set(data,'psi',k);
setappdata(gcbf,'data',data);


%% ------------- Private Functions ------------------------------------------------


function plotkernel(wzrd,k)

try  
  cs = get(getappdata(wzrd,'data'),'CS');
  ma = rotangle_max_z(cs);
  omega = linspace(-ma/2,ma/2,5000);
  
  handles = getappdata(wzrd,'handles');

  v = eval(k,omega); %#ok<EVLC>
  plot(handles.kernelAxis,omega/degree,v,'linewidth',2);
  set(handles.kernelAxis,'ylim',[min([0,v]),max(v)],'yTick',[]);
  set(handles.kernelAxis,'xlim',[min(omega)/degree,max(omega)/degree]);
catch %#ok<CTCH>
end
